
local MainScene = class("MainScene", cc.load("mvc").ViewBase)

function MainScene:run(no)
   self["action" .. no](self)
end

-- Follow
function MainScene:action1()
    local sprite1 = display.newSprite("1.png")
        :move(display.cx, display.cy)
        :addTo(self.backgroundLayer)

    local moveLeft = cc.MoveBy:create(1.5, cc.p(display.width / 2, 0))
    local moveRight = cc.MoveBy:create(5, cc.p(-display.width, 0))
    local seq = cc.Sequence:create(moveLeft, moveRight, moveLeft)
    local rep = cc.RepeatForever:create(seq)
    sprite1:runAction(rep)
    
    self.backgroundLayer:runAction(cc.Follow:create(sprite1)) -- 注释此行来查看不同的效果
end

-- MoveBy
function MainScene:action2()
    local sprite2 = display.newSprite("2.png")
        :move(display.cx, display.cy)
        :addTo(self.backgroundLayer)

    local flipxAction = cc.FlipX:create(true)
    local moveTo = cc.MoveBy:create(1, cc.p(-300, 0))
    local action = cc.Sequence:create(moveTo, flipxAction, moveTo:reverse())
    sprite2:runAction(action)
end

-- Hide
function MainScene:action3()
    local hideAction = cc.Hide:create()
    local moveTo = cc.MoveTo:create(1.5, cc.p(60, 60))
    local action = cc.Sequence:create(moveTo, hideAction)   

    local sprite1 = display.newSprite("1.png")
        :move(display.cx, display.cy)
        :addTo(self.backgroundLayer)
        :runAction(action)
end

-- CallFunc
function MainScene:action4()
    local callback = cc.CallFunc:create(function() print("In callback function") end)
    local moveTo = cc.MoveTo:create(2, cc.p(0, 0))
    local action = cc.Sequence:create(moveTo, callback)

    local sprite1 = display.newSprite("1.png")
        :move(display.cx, display.cy)
        :addTo(self.backgroundLayer)
        :runAction(action)
end

-- 贝塞尔曲线
function MainScene:action5()
    local action = cc.BezierTo:create(2, {cc.p(display.right, display.top), cc.p(200, 200), cc.p(50, 100)}) 

    local sprite1 = display.newSprite("1.png")
        :move(0, 0)
        :addTo(self.backgroundLayer)
        :runAction(action)
end

-- FadeTo
function MainScene:action6()
    local action = cc.FadeTo:create(2, 0)

    local sprite1 = display.newSprite("1.png")
        :move(display.cx, display.cy)
        :addTo(self.backgroundLayer)
        :runAction(action)
end

-- 帧动画
function MainScene:action7()
    local frames = display.newFrames("grossini_dance_%02d.png", 1, 14)
    local animation = display.newAnimation(frames, 0.2)
    local animate = cc.Animate:create(animation)

    local sprite1 = display.newSprite("#grossini_dance_01.png")
        :move(display.cx, display.cy)
        :addTo(self.backgroundLayer)
        :runAction(animate)
end

-- 延迟动作
function MainScene:action8()
    local move = cc.MoveBy:create(1, cc.vertex2F(150, 0))
    local action = cc.Sequence:create(move, cc.DelayTime:create(2), move:reverse())

    local sprite1 = display.newSprite("1.png")
        :move(display.cx, display.cy)
        :addTo(self.backgroundLayer)
        :runAction(action)
end

-- 变速动作
function MainScene:action9()
    local action = cc.EaseSineOut:create(cc.MoveBy:create(2, cc.p(300, 0)))
    local sprite1 = display.newSprite("1.png")
        :move(display.cx, display.cy)
        :addTo(self.backgroundLayer)
        :runAction(action)
end

function MainScene:onCreate()
    self.backgroundLayer = display.newLayer(cc.c4b(128, 128, 128, 255))
    self.backgroundLayer:addTo(self)

    -- preload frames to cache
    display.loadSpriteFrames("grossini-aliases.plist", "grossini-aliases.png")
    
    -- run actions: 1 to 9
    self:run(9)
end

return MainScene
